﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using UDS.Components;

namespace UDS.SubModule.CM.Stat
{
    /// <summary>
    /// Contact_Client 的摘要说明。
    /// </summary>
    public partial class Contact_ClientPage : Page
    {
        protected override void OnInit(EventArgs e)
        {
            btn_OK.Click += btn_OK_Click;
            base.OnInit(e);
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            var cm = new Components.CM();
            var ds = new DataSet();

            DateTime begintime = DateTime.Parse(tbx_begintime.Text);
            DateTime endtime = DateTime.Parse(tbx_endtime.Text);

            SqlDataReader dr_sellman = cm.GetSellman(begintime, endtime);
            DataTable dt_sellman = Tools.ConvertDataReaderToDataTable(dr_sellman);
            dt_sellman.TableName = "Sellman";
            ds.Tables.Add(dt_sellman);
            ltl_AddContactSellman.Text = dt_sellman.Rows.Count.ToString();

            SqlDataReader dr_contact = cm.GetContactInfo(begintime, endtime);
            DataTable dt_contact = Tools.ConvertDataReaderToDataTable(dr_contact);
            dt_contact.TableName = "Contact";
            ds.Tables.Add(dt_contact);
            ltl_Contact.Text = dt_contact.Rows.Count.ToString();

            SqlDataReader dr_callincontact = cm.GetCallinContactInfo(begintime, endtime);
            DataTable dt_callincontact = Tools.ConvertDataReaderToDataTable(dr_callincontact);
            dt_callincontact.TableName = "CallinContact";
            ds.Tables.Add(dt_callincontact);
            ltl_CallinContact.Text = dt_callincontact.Rows.Count.ToString();

            SqlDataReader dr_newclient = cm.GetNewClient(begintime, endtime);
            DataTable dt_newclient = Tools.ConvertDataReaderToDataTable(dr_newclient);
            dt_newclient.TableName = "NewClient";
            ds.Tables.Add(dt_newclient);
            ltl_NewClient.Text = dt_newclient.Rows.Count.ToString();

            SqlDataReader dr_neoclient = cm.GetNegotiateClient();
            DataTable dt_neoclient = Tools.ConvertDataReaderToDataTable(dr_neoclient);
            dt_neoclient.TableName = "NeoClient";
            ds.Tables.Add(dt_neoclient);
            ltl_NegotiateClient_Total.Text = dt_neoclient.Rows.Count.ToString();

            SqlDataReader dr_neonewclient = cm.GetNewNegotiateClient(begintime, endtime);
            DataTable dt_neonewclient = Tools.ConvertDataReaderToDataTable(dr_neonewclient);
            dt_neonewclient.TableName = "NeoNewClient";
            ds.Tables.Add(dt_neonewclient);
            ltl_NegotiateClient_New.Text = dt_neonewclient.Rows.Count.ToString();

            SqlDataReader dr_new3client = cm.GetNew3StarClient(begintime, endtime);
            DataTable dt_new3client = Tools.ConvertDataReaderToDataTable(dr_new3client);
            dt_new3client.TableName = "New3Client";
            ds.Tables.Add(dt_new3client);
            ltl_New3Client_New.Text = dt_new3client.Rows.Count.ToString();


            SqlDataReader dr_3client = cm.Get3StarClient();
            DataTable dt_3client = Tools.ConvertDataReaderToDataTable(dr_3client);
            dt_3client.TableName = "3Client";
            ds.Tables.Add(dt_3client);
            ltl_New3Client_Total.Text = dt_3client.Rows.Count.ToString();

            int fee = 0;
            int feetimes = 0;
            foreach (DataRow row in dt_contact.Rows)
            {
                if (row["Fee"].ToString() != "0")
                {
                    fee += Int32.Parse(row["fee"].ToString());
                    feetimes++;
                }
            }
            ltl_Fee.Text = fee.ToString();
            ltl_FeeTimes.Text = feetimes.ToString();

            SqlDataReader dr_feeclient = cm.GetFeeBySellmanID(0, begintime, endtime);
            DataTable dt_feeclient = Tools.ConvertDataReaderToDataTable(dr_feeclient);
            dt_feeclient.TableName = "FeeClient";
            ds.Tables.Add(dt_feeclient);

            ltl_FeeClient.Text = dt_feeclient.Rows.Count.ToString();
            dt_sellman.DefaultView.RowFilter = "Fee>0";
            ltl_FeeSellman.Text = dt_sellman.DefaultView.Count.ToString();
        }
    }
}